<!DOCTYPE html>
<html class="x-admin-sm">

    <head>
        <meta charset="UTF-8">
        <title>资源管理</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<!--        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />-->
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/xadmin.js"></script>
        <script type="text/javascript" src="./js/jquery.min.js"></script>
        <script type="text/javascript" src="./data/sensor.js"></script>
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
 <style>
     .layui-table-cell {
         text-align: center;
         height: auto;
         white-space: normal;
     }

     .layui-table img {
         max-width: 80px;
     }
 </style>
    </head>

    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">煤矿记录</a>
                <a>
                    <cite>资源记录</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
<!--                        <div class="layui-card-body ">-->
<!--                            <form class="layui-form layui-col-space5">-->
<!--                                <div class="layui-inline layui-show-xs-block">-->
<!--                                    <input class="layui-input" autocomplete="off" placeholder="开始日" name="start" id="start"></div>-->
<!--                                <div class="layui-inline layui-show-xs-block">-->
<!--                                    <input class="layui-input" autocomplete="off" placeholder="截止日" name="end" id="end"></div>-->
<!--                                <div class="layui-inline layui-show-xs-block">-->
<!--                                    <input type="text" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input"></div>-->
<!--                                <div class="layui-inline layui-show-xs-block">-->
<!--                                    <button class="layui-btn" lay-submit="" lay-filter="sreach">-->
<!--                                        <i class="layui-icon">&#xe615;</i></button>-->
<!--                                </div>-->
<!--                            </form>-->
<!--                        </div>-->
                        <div class="layui-card-body ">
<!--                            <table class="layui-table" lay-data="{url:'./user.json',page:true,toolbar: '#toolbarDemo',id:'test'}" lay-filter="test">-->
<!--                                <thead>-->
<!--                                    <tr>-->
<!--                                        <th lay-data="{type:'checkbox'}">ID</th>-->
<!--                                        <th lay-data="{field:'pid', width:80, sort: true}">ID</th>-->
<!--                                        <th lay-data="{field:'deviceId', width:120, sort: true, edit: 'text'}">设备标识</th>-->
<!--                                        <th lay-data="{field:'deviceIp', edit: 'text', minWidth: 150}">设备IP</th>-->
<!--                                        <th lay-data="{field:'deviceLocation', width:80,templet: '#switchTpl'}">位置信息</th>-->
<!--                                        <th lay-data="{field:'city', edit: 'text', minWidth: 100}">城市</th>-->
<!--                                        <th lay-data="{field:'experience', sort: true, edit: 'text'}">积分</th>-->
<!--                                        <th lay-data="{field:'dw_xinzhi',templet: function(d){ return d.dw_xinzhi.titel;}}">学校</th></tr>-->
<!--                                </thead>-->
<!--                            </table>-->
                            地点: <select id="select_address" style="width: 100px;height: 20px">
                                    <option value="0">请选择</option>
                            </select>
                            &nbsp;&nbsp;文档类别: <select id="select_attachmentType" style="width: 100px;height: 20px">
                                <option value="0">请选择</option>
                            </select>
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" lay-submit="" lay-filter="sreach" onclick="searchInfo()">
                                    <i class="layui-icon">&#xe615;</i></button>
                            </div>
<!--                            <a  onclick="showDetailsByMin()" style="color: #00a0e9; text-decoration:underline;"> 查看详细</a>-->
                            <table id="demo" class="layui-table" lay-filter="test_table" ></table>
                            <div id="test1"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div id="box1" style="width:400px;height:400px;display: none">
           <input type="file" id="file_1" >
            <button type="button" class="layui-btn" id="saveFile" onclick="saveFileInfo()"><i class="layui-icon"></i>上传</button>

            <div id="showFileListInfo"></div>
        </div>
    </body>
    <script type="text/html" id="toolbarMenu">
        <div class = "layui-btn-container"  id="tableMenu">
            <!--            <button class = "layui-btn layui-btn-sm" lay-event = "getCheckData" > 获取选中行数据 </button>-->
            <!--            <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button >-->

            <button class = "layui-btn layui-btn-sm" lay-event = "addSensor" onclick="xadmin.open('添加文档','./ai_mines_attachment_AddOrEdit.html?id=-1',600,400)" ><i class="layui-icon"></i> 添加记录</button>

            <!--            <button class = "layui-btn layui-btn-danger" lay-event = "delCheckData" > <i class="layui-icon"></i>删除选中 </button>-->
<!--            <button type="button" class="layui-btn" id="test3"><i class="layui-icon"></i>上传文件</button>-->

        </div >
    </script>
    <script type="text/html" id="toolbarDemo">
        <div class = "layui-btn-container" >
<!--            <button class = "layui-btn layui-btn-sm" lay-event = "getCheckData" > 获取选中行数据 </button>-->
<!--            <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button >-->
<!--            <button class = "layui-btn layui-btn-sm" lay-event = "isAll" > 验证是否全选</button>-->
<!--            <button class = "layui-btn layui-btn-danger"   lay-event = "delete" > 删除</button>-->

<!--            <button type="button" class="layui-btn"><i class="layui-icon"></i></button>-->
<!--            <img src="images/att.png" style="width: 15px; height: 15px;" lay-enent="uploadFile">-->
<!--            <i class="layui-icon layui-icon-edit"    lay-event = "edit" >&nbsp;&nbsp;</i>-->
            {{# if(d.status==0){ }}
            <i class="layui-icon layui-icon-delete"  lay-event = "delete" >&nbsp;&nbsp;</i>
            <button  class="layui-btn  layui-btn-normal" lay-event = "lockData" >🔒</button>
            {{# } else{}}
            <i class="layui-icon"  lay-event = "" onclick="layer.msg('已锁定！')">🔒</i>
            {{# } }}


            <!--            <i class="layui-icon layui-icon-upload"  lay-event = "uploadFile" >&nbsp;&nbsp;</i>-->
        </div >


    </script>

    <script type="text/javascript">

        // setInterval(function(){
        //    // initData();
        // },1000);
        function showDetailsByMin(){
            xadmin.open('详细信息','warningList.html');
        }

        $(function () {
             initSelectData();
             searchInfo();
        });

        function searchInfo(){
            initData();
        }
        function  initSelectData() {
            var dataSensor=sensorServer.getMinesList({"page":1,"rows":1000});
            if (dataSensor!=null) {
                let optionList="";
                for(let i=0;i<dataSensor.data.length;i++){
                    optionList+="<option value='"+dataSensor.data[i].id+"'>"+dataSensor.data[i].mineName+"</option>";
                }
                $("#select_address").append(optionList);
            }

            var dataType=sensorServer.getMinesAttachmentType({"page":1,"rows":1000});
            if (dataType!=null) {
                let optionList="";
                for(let i=0;i<dataType.data.length;i++){
                    optionList+="<option value='"+dataType.data[i].id+"'>"+dataType.data[i].typeName+"</option>";
                }
                $("#select_attachmentType").append(optionList);
            }
        }
        //请求数据
        function getData(page,limit) {
            var resultData;
            let minesId=$("#select_address").val();
            let typeId=$("#select_attachmentType").val();
            var params={"page":page,"rows":limit,"minesId":minesId,"typeId":typeId}
            resultData = sensorServer.getMinesAttachmentList(params);
            return resultData;
        }

        function saveFileInfo() {
            let formData = new FormData();
            formData.append("file", document.getElementById('file_1').files[0]);
            let url=sensorServer.getUploadFileUrl();
            $.ajax({
                url:url,
                type:"post",
                data:formData,
                processData:false,
                contentType:false,
                success:function(data){
                    console.log(JSON.stringify(data));
                    debugger;
                    if(data.msg=="操作成功"&&data.data!=null){
                        let uploadSuccessPath=data.data;
                        if(uploadSuccessPath!="") {
                            let fileName=document.getElementById('file_1').files[0].name;
                            let serverUrl=data.data;
                            let params={"id":uploadDeliverRecordId,"url":uploadSuccessPath};
                            let result=sensorServer.updateProcessRecord(params);
                            if(result==1)
                               layer.msg("上传成功！");
                            else
                               layer.msg("操作失败！");
                        }else{
                            layer.msg("保存失败！");
                        }
                    }else{
                        alert(JSON.stringify(data));
                        layer.msg("上传失败！");
                    }
                },
                error:function(e){
                    alert("错误！！");

                }
            });
        }
        let uploadDeliverRecordId=0;
        function refData(dataList){
            //var dataList=dataArraygetData(page,limit).data;
            layui.use('table', function () {
                var table = layui.table;
                //第一个实例
                table.render({
                    elem: '#demo'
                    ,toolbar: "#toolbarMenu"
                    ,height: 450
                    ,data: dataList
                    ,limit:dataList.length
                    //page:false
                    ,cols: [[ //表头
                        {field: 'id', title: '编号', width: 80} //, sort: true, fixed: 'left'
                        , {field: 'mineName', title: '煤矿名称'}
                        , {field: 'typeName', title: '类别'}
                        , {field: 'fileName', title: '名称'}
                        , {field: 'url', title: '资源'} //,templet:'<div><img onclick="showBgPic(this)" src="{{d.url}}" ></div>'
                        , {field: 'note', title: '备注'}
                        , {field: 'createTime', title: '时间'}
                        , {field: '', title: '编辑', width: 250, sort: true, toolbar:'#toolbarDemo'}
                    ]],
                    loading:true,
                    done:function (res, curr, count) {
                        $("[data-field='status']").children().each(function(){
                            if($(this).text()==0){
                                $(this).text("告警中")
                            }else if($(this).text()==1){
                                $(this).text("结束")
                            }else if($(this).text()==2){
                                $(this).text("已处理")
                            }
                        });
                        $("[data-field='url']").children().each(function(){
                            if($(this).text()!="资源"){
                                let urlText=$(this).text();  //href="'+urlText+'"
                                let content='<a onclick="showFile(this)" data-url="'+urlText+'" ><i class="layui-icon layui-icon-file-b" style="font-size: 30px; color: #1E9FFF;"></i>  </a>';
                                $(this).html(content);
                                //
                            }
                        });
                        // $("[data-field='attachmentList']").children().each(function(e){
                        //     if($(this).text()=="[object Object]"){
                        //         $(this).text("查看附件");
                        //     }
                        // });
                    },
                    parseData:function(res){
                        return {
                            "code" : res.code, //解析接口状态
                            "msg" : res.msg, //解析提示文本
                            "data" : res.data //解析数据列表
                        }
                    }
                });

                //监听单元格编辑
                // table.on('edit(test_table)',
                //     function (obj) {
                //         var value = obj.value //得到修改后的值
                //             ,
                //             data = obj.data //得到所在行所有键值
                //             ,
                //             field = obj.field; //得到字段
                //         layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);
                //         //updateTable(data.pid,value);
                //         var jsonResult = sensorServer.updateSensorInfo(obj.data);
                //         console.log(jsonResult);
                //     });

                //监听行单击事件（双击事件为：rowDouble）
                // table.on('row(test_table)', function(obj){
                //     var data = obj.data;
                //     layer.alert(JSON.stringify(data), {
                //         title: '当前行数据：'
                //     });
                //
                //     //标注选中样式
                //     obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
                // });

                //列里的工具栏
                table.on('tool(test_table)',
                    function(obj) {
                        //var checkStatus = table.checkStatus(obj.config.pid);
                        switch (obj.event) {
                            case 'delete':
                                layer.msg('data:'+obj.data.id);
                                del(obj);
                                break;
                            case 'edit':
                                layer.msg('data:'+obj.data.id);
                                xadmin.open('修改工艺流程拍照','./ai_mines_attachment_AddOrEdit.html'+"?id="+obj.data.id,900,600);
                                break;
                                //updateWarningInfo(obj);
                                //break;
                            case 'uploadFile':
                                layer.msg('data:'+obj.data.id);
                                uploadDeliverRecordId=obj.data.id;
                                showUploadFile();
                                break;
                            case 'showPic':
                                    // console.log(obj.data.id);
                                    // showUploadFile();
                                    // debugger;
                                    // $("#showFileListInfo").html("");
                                    // let html="";
                                    // if(obj.data.attachmentList.length>0){
                                    //     for (let  i=0;i<obj.data.attachmentList.length;i++){
                                    //         html+="<img style='width: 50px; height: 50px;' src='"+obj.data.attachmentList[i].url+"' />";
                                    //     }
                                    // }
                                    // $("#showFileListInfo").html(html);
                                    break;
                            case 'lockData':
                                let msg="确定要锁定吗？锁定后不可进行任何更改！";
                                layer.confirm(msg,function(index) {
                                    obj.data.status=1;
                                    let result=sensorServer.updateMinesAttachment(obj.data);
                                    if(result==1){
                                        layer.msg('已锁定!',{icon:1,time:1000});
                                        location.reload();
                                    }else{
                                        layer.msg('锁定失败!',{icon:2,time:1000});
                                    }
                                });
                                break;
                        };
                    });
            });
        }

        function initData(){
            var initData=getData(1,10);
            var total=initData.total;
            var dataList=initData.data;
            layui.use('table', function(){
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
                    ,count: total //数据总数，从服务端得到
                    //,limit:10
                    ,limits:[10,20,30]
                    ,curr:1
                    ,group:5
                    ,layout: ['prev', 'page', 'next','limit','refresh','skip','count']
                    ,jump: function(obj, first){
                        //obj包含了当前分页的所有参数，比如：
                        console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                        console.log(obj.limit); //得到每页显示的条数
                        //首次不执行
                        if(!first){
                            refData(getData(obj.curr,obj.limit).data);
                        }else{
                            refData(dataList);
                        }

                    }
                });
            });
        }


        //删除
        function del(obj) {
            let msg="您确定要删除吗？";
            layer.confirm(msg,function(index){
                var params=obj.data;
                var resultData = sensorServer.delMinesAttachment(params);
                if(resultData==1){
                    location.reload();
                }
            });
        }

        function  updateWarningInfo(obj){
            obj.data.active=0;
            msg="确认已阅读此警示，并进行相关处理吗？";
            layer.confirm(msg,function(index){
                var jsonResult = sensorServer.updateWarningInfo(obj.data);
                layer.msg('已设置!',{icon:1,time:1000});
                location.reload();
            });
        }

        function showUploadFile(){
            alert(1);
            layer.open({
                type: 1,
                closeBtn: 1,
                shift: 2,
                shadeClose: true,
                content: $("#box1")
            });
        }

        function showBgPic(img) {
            let content='<img src="'+img.src+'" />';
            if(".mp4"==img.src.substr(img.src.length-4)){
                content='<video width="580px" height="380px" src="'+img.src+'" controls="controls">\n' +
                    '您的浏览器不支持 video 标签。\n' +
                    '</video>';
            }
            layer.open({
                type: 1,
                closeBtn: 1,
                shift: 2,
                area:['600px','400px'],
                shadeClose: true,
                content:content
            });

        }
        function  showFile(va) {
            open($(va).data("url"));
        }
    </script>


</html>
